const util = require("../../utils/utils");
const toast = require("../../utils/toasts");
const wxm = require("../../utils/wechat_modules");

import {
  card_shareimg,
  card_sharecomimg,
  card_shareshopimg,
  card_shareproimg,
  card_shareprimg,
  card_shareagentimg,
  card_sharewxgzhimg,
  card_shareproptimg,
  card_shareprocutimg,
  card_sharecoopimg
} from "../../utils/api.js";

Component({
  properties: {
    actionSheetHidden: {
      type: Boolean, //必填，目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: true //可选，默认值，如果页面没传值过来就会使用默认值
    },
    isIphoneX: {
      type: Boolean, //必填，目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: false //可选，默认值，如果页面没传值过来就会使用默认值
    },
    showShareImg: {
      type: Boolean, //必填，目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: false //可选，默认值，如果页面没传值过来就会使用默认值
    },
    shareImg: {
      type: String, //必填，目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
    },
    user: {
      type: Object, //必填，目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: {} //可选，默认值，如果页面没传值过来就会使用默认值
    },
    cardId:Number,
    sellerId: Number,
    productId: Number,
    shopId:Number,
    type:String
  },
  attached: function () {
    this.showErCode();
  },
  methods: {
    dataInit() {
      let active = this.properties.active;
      this.setData({
        filterActive: active
      })
    },
    listenerActionSheet: function() {
      this.hide();
    },
    showErCode() {
      if (this.data.showShareImg) {
        // this.draw();
        if (this.data.type == 'com') {
          this.getShareComImg();

        } else if (this.data.type == 'shop') {
          this.getShareShopImg();

        } else if (this.data.type == 'pro') {
          this.getShareProImg();
        } else if (this.data.type == 'propt') {
          this.getShareProPtImg();
        } else if (this.data.type == 'procut') {
          this.getShareProCutImg();
        } else if (this.data.type == 'agentPr') {
          this.getSharePrImg();
        } else if (this.data.type == 'coop') {
          this.getShareCoopImg();
        } else if (this.data.type == 'agent') {
          this.getShareAgentImg();
        } else if (this.data.type == 'own') {
          this.getShareWxgzhImg();
        } else {
          this.getShareImg();
        }
      }
    },
    hide() {
      this.setData({
        actionSheetHidden: true,
        showShareImg: false
      });
      util.hideModal();
      this.triggerEvent('close', {});
    },
    hideAs:function() {
      this.setData({
        actionSheetHidden: true,
      });
    },
    clickItem: function(e) {
      var index = e.currentTarget.dataset.index;
      if(index == 1 || index == 2) {
        this.setData({
          showShareImg:true,
          index:index
        });
        this.showErCode();
        this.hideAs();
      } else {
        this.hide();
      }
    },
    draw: function() {
      var that = this;
      util.showModal();
      wx.showModal({
        title: '二维码已生成',
        content: '小提示：点击立即分享后可以长按图片，选择转发',
        cancelText: '保存相册',
        confirmText: '立即分享',
        confirmColor: '#577bef',
        success: (res) => {
          if (res.confirm) {
            if (that.data.shareImg) {
              wx.previewImage({
                urls: [that.data.shareImg],
              })
            }
          } else {
            that.saveImg(that.data.shareImg);
          };
          that.hide();
        }
      })
    },
    saveImg: function(imgSrc) {
      var that = this;
      wx.downloadFile({
        url: imgSrc,
        success: function(res) {
          console.log(res);
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function(data) {
              wxm.showModal({
                title: '保存成功',
                content: '二维码图片已经保存到相册中，你可以直接分享到朋友圈或好友群组',
                hideCancel: true,
                isIt: (true)
              }).then(() => {
              }).catch(() => {});
            },
            fail: function(err) {
              console.log(err);
              if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                console.log("当初用户拒绝，再次发起授权")
                wx.openSetting({
                  success(settingdata) {
                    console.log(settingdata)
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                    } else {
                      console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                    }
                  }
                })
              }
            },
            complete(res) {
              console.log(res);
            }
          })
        }
      })
    },
    getShareImg:function() {
      if(!this.data.cardId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareimg({
        id: this.data.cardId
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg:res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareProImg: function () {
      if (!this.data.productId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareproimg({
        id: this.data.productId,
        salesId: this.data.cardId ? this.data.cardId : ''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareProPtImg: function () {
      if (!this.data.productId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareproptimg({
        id: this.data.productId,
        salesId: this.data.cardId ? this.data.cardId : ''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareProCutImg: function () {
      if (!this.data.productId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareprocutimg({
        id: this.data.productId,
        salesId: this.data.cardId ? this.data.cardId : ''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getSharePrImg: function () {
      if (!this.data.cardId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareprimg({
        agentId: this.data.cardId
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareCoopImg: function () {
      toast.showLoading();
      var that = this;
      card_sharecoopimg({}).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareComImg: function () {
      if (!this.data.sellerId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_sharecomimg({
        id: this.data.sellerId,
        salesId: this.data.cardId ? this.data.cardId:''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareAgentImg: function () {
      if (!this.data.sellerId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareagentimg({
        sellerId: this.data.sellerId,
        salesId: this.data.cardId ? this.data.cardId : ''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    },
    getShareShopImg: function () {
      if (!this.data.shopId) {
        return;
      }
      toast.showLoading();
      var that = this;
      card_shareshopimg({
        id: this.data.shopId,
        salesId: this.data.cardId ? this.data.cardId : ''
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        }
      });
    },
    getShareWxgzhImg: function () {
      toast.showLoading();
      var that = this;
      var index = this.data.index;
      card_sharewxgzhimg({
        size:index
      }).then(res => {
        if (res.success) {
          that.setData({
            shareImg: res.result
          });
          that.draw();
        } else {
          that.hide();
        }
      });
    }
  }
});